import fa from "element-ui/src/locale/lang/fa";

var timer=null;
var callbacks=[];
function start(interval) {
    if (timer==null) {
        timer = setInterval(() => {
            // 某些定时器操作
            try {
                for (var i = callbacks.length - 1; i >= 0; i--) {
                    callbacks[i].func(callbacks[i].host)
                }
            } catch (e) {
                console.log("setInterval: ", e);
            }
        }, interval);
    }
}

function stop() {
    if (timer!=null){
        clearInterval(timer);
        timer=null;
    }
    try {
        callbacks=[]
    }catch (e) {
        console.log("clearInterval: ", e);
    }
}

function addCallBack(host,func) {
    start(1000*60*5)
    if (func!=null&&host!=null){
        try {
            var find=false;
            for (var i = callbacks.length - 1; i >= 0; i--) {
                if (callbacks[i].host==host){
                    callbacks[i].func=func;
                    find=true;
                    break
                }
            }
            if (!find){
                callbacks.push({
                    host:host,
                    func:func
                })
            }
        } catch (e) {
            console.log("setInterval: ", e);
        }
    }
}
export default
{
    start,
    stop,
    addCallBack
}